﻿// Type: GandA.Component.Log.Log
// Assembly: GandA.Component.Log, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// Assembly location: C:\CodePlex\CLab\CLab\DLLs\GandA.Component.Log.dll

using System;
using System.IO;
using System.Reflection;
using System.Configuration;

namespace GandA.Component.Log
{
    public static class Log
    {
        public static void WriteLog(string exception, string desc)
        {
            Log.WriteLog(exception, (Exception)null, desc);
        }

        public static void WriteLog(Exception exception, string desc)
        {
            Log.WriteLog((string)null, exception, desc);
        }

        private static void WriteLog(string msgException, Exception exception, string desc)
        {
            string path = ConfigurationManager.AppSettings["PathLog"].ToString() + "\\log.txt";//Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "").Replace(@"\bin", "") + "\\log.txt";
                StreamWriter streamWriter = new StreamWriter(!File.Exists(path) ? (Stream)new FileStream(path, FileMode.Create, FileAccess.Write) : (Stream)new FileStream(path, FileMode.Append, FileAccess.Write));
                streamWriter.WriteLine("\n");
                streamWriter.WriteLine("**** Error en --> [" + desc + "] " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + "****");
                if (!string.IsNullOrEmpty(msgException))
                    streamWriter.WriteLine(msgException);
                else if (exception != null)
                {
                    streamWriter.WriteLine(exception.Message);
                    if (exception.InnerException != null)
                        streamWriter.WriteLine(exception.InnerException.Message);
                }
                streamWriter.Close();
        }
    }
}
